module SDLBridge.SDLSound;
private import SDL;
private import SDL_mixer;
private import System.Console;
private import Sound.Manager;
private import std.string;
private import std.math;


public class SDLSoundSprite : ISoundSprite {
	private float _x , _y , _z;
	private static int _IDCNT = 0;
	private int _id;
	
	private bool _busy , _visible;
	private int _volume;
	private string _src;
	private int _repeat;

	private Mix_Chunk* _sound;
	private static Mix_Chunk[ string ] _pool;

	public float x(){ return _x; }
	public float y(){ return _y; }
	public float z(){ return _z; }
	public void x( float xx ){ _x = xx; }
	public void y( float yy ){ _y = yy; }
	public void z( float zz ){ _z = zz; }

	public bool visible(){ return _visible; }
	public int volume(){ return _volume; }
	public void visible( bool f ){ _visible = f; }
	public void  volume( int v ){
		if ( v <   0 ) v =   0;
		if ( v > 100 ) v = 100;
		_volume = v;
		int sdlvol = cast(int)round( 128.0 * ( v / 100.0 ) );
		Mix_Volume( _id , sdlvol );
	} //

	public this() {
		_x = 0;
		_y = 0;
		_z = 0;
		_id = _IDCNT++;
		_busy = false;
		_visible = true;
		_repeat = 0;
	} //


	public bool busy(){ 
		return _busy; 
	} //

	public void load ( string src ) {
		if ( _src != src ) {
			if ( !(src in _pool) ) {
				_sound = Mix_LoadWAV( toStringz(src) );
				_pool[ src ] = *_sound;
			} else
				_sound = &_pool[ src ] ;
		}
	} //

	public string src() { return _src; }

	public ISoundSprite play( string src , int repeat ) {
		_repeat = repeat;
		load(src);
		play();
		return this;
	} //
	public ISoundSprite play( string src ) {
		load(src);
		play();
		return this;
	} //
	public ISoundSprite play() {
		if ( _sound ) {
			Mix_PlayChannel( _id , _sound , _repeat );
			volume = _volume;
		}
		return this;
	} //

	public int repeat(){
		return _repeat;
	} //
	public void repeat( int v ){
		_repeat = v;
	} //
	
	public ISoundSprite stop(){
		if ( _sound ) {
			Mix_HaltChannel( _id );
		}
		return this;
	} //
	public ISoundSprite pause(){
		Mix_Pause( _id );
		return this;
	} //
	public ISoundSprite resume(){
		Mix_Resume( _id );
		return this;
	} //
	public ISoundSprite reset(){
		_repeat = 0;
		stop();
		return this;
	} //
	public ISoundSprite release(){
		_busy = false;
		return this;
	} //
	public ISoundSprite call(){
		_busy = true;
		return this;
	} //

	public ISoundSprite moveTo( float xx , float yy){
		return this;
	} //
	
} //



public class SDLBackgroundMusic : ISoundSprite {
	public static SDLBackgroundMusic instance;// = new SDLBackgroundMusic();

	private float _x , _y , _z;
	private static int _IDCNT = 0;
	private int _id;
	
	private bool _busy , _visible;
	private int _volume;
	private string _src;
	private int _repeat;

	private Mix_Music* _sound;
	private static Mix_Music[ string ] _pool;

	public float x(){ return _x; }
	public float y(){ return _y; }
	public float z(){ return _z; }
	public void x( float xx ){ _x = xx; }
	public void y( float yy ){ _y = yy; }
	public void z( float zz ){ _z = zz; }

	public bool visible(){ return _visible; }
	public int volume(){ return _volume; }
	public void visible( bool f ){ _visible = f; }
	public void  volume( int v ){
		if ( v <   0 ) v =   0;
		if ( v > 100 ) v = 100;
		_volume = v;
		int sdlvol = cast(int)round( 128.0 * ( v / 100.0 ) );
		Mix_VolumeMusic( sdlvol );
	} //

	private static this() {
		instance = new SDLBackgroundMusic();
	}

	private this() {
		_x = 0;
		_y = 0;
		_z = 0;
		_id = _IDCNT++;
		_busy = false;
		_visible = true;
		_repeat = -1;
	} //
	


	public bool busy() {
		return _busy || Mix_PlayingMusic( ) > 0; 
	} //

	public void load ( string src ) {
		_sound = Mix_LoadMUS( toStringz( src ) );
		/*
		if ( _src != src ) {
			if ( !(src in _pool) ) {
				_sound = Mix_LoadMUS( toStringz( src ) );
				_pool[ src ] = *_sound;
			} else
				_sound = &_pool[ src ] ;
		}
		*/
	} //

	public string src() { return _src; }

	public ISoundSprite play( string src , int repeat ) {
		_repeat = repeat;
		load(src);
		play();
		return this;
	} //
	public ISoundSprite play( string src ) {
		load(src);
		play();
		return this;
	} //
	public ISoundSprite play() {
		if ( _sound ) {
			Mix_PlayMusic( _sound , _repeat );
		}
		return this;
	} //

	public int repeat(){
		return _repeat;
	} //
	public void repeat( int v ){
		_repeat = v;
	} //
	
	public ISoundSprite stop(){
		if ( _sound ) {
			Mix_HaltMusic( );
		}
		return this;
	} //
	public ISoundSprite reset(){
		_repeat = 0;
		stop();
		return this;
	} //
	public ISoundSprite pause(){
		Mix_PauseMusic( );
		return this;
	} //
	public ISoundSprite resume(){
		Mix_ResumeMusic( );
		return this;
	} //
	public ISoundSprite release(){
		_busy = false;
		return this;
	} //
	public ISoundSprite call(){
		_busy = true;
		return this;
	} //

	public ISoundSprite moveTo( float xx , float yy){
		return this;
	} //
	

} //